import { defineComponent } from 'vue';
import { useNamespace } from '@ibiz-template/vue3-util';
import './grid-field-column.scss';
import { GridRowState } from '../../grid/grid-row.state';
import { GridFieldColumnController } from './grid-field-column.controller';

export const GridFieldColumn = defineComponent({
  name: 'IBizGridFieldColumn',
  props: {
    controller: {
      type: GridFieldColumnController,
      required: true,
    },
    row: {
      type: GridRowState,
      required: true,
    },
  },
  setup(props) {
    const ns = useNamespace('grid-field-column');

    /**
     * 单元格点击事件
     *
     * @author lxm
     * @date 2022-09-28 18:09:27
     * @param {MouseEvent} event
     */
    const onCellClick = (event: MouseEvent): void => {
      if (props.controller.hasAction) {
        event.stopPropagation();
        // 阻止触发行点击
        props.controller.triggerAction(props.row, event);
      }
    };

    /**
     * 文本点击事件
     *
     * @author zk
     * @date 2023-07-13 12:07:53
     * @param {MouseEvent} event
     */
    const onTextClick = (event: MouseEvent): void => {
      // 阻止触发行点击
      if (props.controller.isLinkColumn) {
        event.stopPropagation();
        props.controller.openLinkView(props.row, event);
      }
    };

    return { ns, onCellClick, onTextClick };
  },
  render() {
    const c = this.controller;
    const fieldValue = this.row.data[this.controller.model.codeName!];
    return (
      <div
        class={[this.ns.b(), c.clickable(this.row) && this.ns.m('clickable')]}
        onClick={this.onCellClick}
      >
        {c.codeList ? (
          <iBizCodeList
            class={this.ns.e('text')}
            codeListItems={c.codeListItems}
            codeList={c.codeList}
            value={fieldValue}
            onClick={this.onTextClick}
          ></iBizCodeList>
        ) : (
          <span class={this.ns.e('text')} onClick={this.onTextClick}>
            {fieldValue}
            {fieldValue != null && c.model.unitName}
          </span>
        )}
      </div>
    );
  },
});
export default GridFieldColumn;
